package com.potevio.simulator.strategy.impl;


import com.potevio.simulator.common.CommonFlag;
import com.potevio.simulator.config.MsgTypeConst;
import com.potevio.simulator.heart.HeartbeatClient;
import com.potevio.simulator.task.ChargeMsgTask;
import com.potevio.simulator.util.HexUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;

/**
 * @ClassName Get3FEnum
 * @Description: 获取3F报文枚举类
 * @Date 2021/6/29  9:12
 * @Author zyd
 */
public enum Get3FEnum {
    cumulativeChargeKwh("cumulativeChargeKwh", getCumulativeChargeKwh()),
    cumulativeChargeAh( "cumulativeChargeAh", getCumulativeChargeAh()),
    cumulativeChargeTime("cumulativeChargeTime", getCumulativeChargeTime()),
    startMeterDegress("startMeterDegress", getStartMeterDegress()),
    endMeterDegress("endMeterDegress", getEndMeterDegress()),
    chargeStartSOC("chargeStartSOC", getChargeStartSOC());

    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    Get3FEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    static Get3FEnum of(String key){
        for(Get3FEnum temp : Get3FEnum.values()){
            if(temp.key.equals(key)){
                return temp;
            }
        }
        return null;
    }

    /**
     * 本次累计充电Kwh
     * @return
     */
    public static String getCumulativeChargeKwh(){
        String value = MsgTypeConst.chm_Map.get("0x3F").get("cumulativeChargeAh");
        return value;
    }

    /**
     * 本次累计充电Ah
     * @return
     */
    public static String getCumulativeChargeAh(){
        BigDecimal quantityElectricity = ChargeMsgTask.quantityElectricity;
        // 累计充电电能=结束电表度数-开始电表度数
        int meterDegrees = quantityElectricity.multiply(new BigDecimal(100)).intValue();
        String value = HexUtil.random2HexAndReverse(meterDegrees);
        return value;
    }

    /**
     * 本次累计充电时间
     * @return
     */
    public static String getCumulativeChargeTime(){
        String startTimeHexStr = null;
        try {
            // 获取充电类型
            String chargeType = HeartbeatClient.chargeType;
            // 处理79报文，从其他报文中获取对应字段值
            if("0".equals(chargeType)){
                if("3.4a".equals(CommonFlag.version) || "3.4a1".equals(CommonFlag.version)){
                    startTimeHexStr = HexUtil.time2Hex(MsgTypeConst.chm_Map.get("0x7A_"+ Receive05Command.chargePortTemp).get("transactionTime"));

                }else{
                    startTimeHexStr = HexUtil.time2Hex(MsgTypeConst.chm_Map.get("0x7A").get("transactionTime"));
                }
            }else if("1".equals(chargeType)){
                if("3.4a".equals(CommonFlag.version) || "3.4a1".equals(CommonFlag.version)){
                    startTimeHexStr = HexUtil.time2Hex(MsgTypeConst.chm_Map.get("0x70_"+CommonFlag.chargeNum).get("transactionTime"));
                }else{
                    startTimeHexStr = HexUtil.time2Hex(MsgTypeConst.chm_Map.get("0x70").get("transactionTime"));
                }

            }
            String value = HexUtil.getTimeStrInterval(startTimeHexStr, Deal79Command.date);
            //

            if (StringUtils.isNotBlank(value) && value.length() < 8) {
                int a = 8 - value.length();
                for (int j = 0; j < a; j++) {
                    value = value + "0";
                }
            }
            return value;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 开始电表度数
     * @return
     */
    public static String getStartMeterDegress(){
        String value = HexUtil.random2HexAndReverse(ChargeMsgTask.InitMeterDegrees*100);
        value = HexUtil.getIntervalByteNum(value, "0x79", "startMeterDegress");
        return value;
    }

    /**
     * 电表度数Kwh
     * @return
     */
    public static String getEndMeterDegress(){
        String value = MsgTypeConst.chm_Map.get("0x3F").get("ammeterDegrees");
        return value;
    }

    /**
     * 开始充电SOC
     * @return
     */
    public static String getChargeStartSOC(){
        String value = MsgTypeConst.chm_Map.get("0x3F").get("chargeStartSOC");
        return value;
    }
}
